package com.llh.designpatterns.factory.simplefactory.demo01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * <h2>车辆订购中心</h2>
 * <p>CreatedAt: 2019-11-25 22:39</p>
 * <p><a href="mailto:forever2y_y@163.com">SendMailToMe</a></p>
 *
 * @author llh
 */
public class OrderCars {
    private void orderCars() {
        Car car = null;
        String carType;
        while (true) {
            carType = carType();
            if (carType.equals("Truck")) {
                car = new Truck();
            } else if (carType.equals("sports")) {
                car = new SportsCar();
            } else {
                break;
            }
            car.run();
            car.alert();
        }
    }


    // 使用工厂类创建
    private void orderCarByFactory() {
        SimpleFactoryCar factoryCar = new SimpleFactoryCar();
        while (true) {
            Car car = factoryCar.createCar(carType());
            if (null != car) {
                car.run();
                car.alert();
            } else {
                break;
            }
        }
    }

    private String carType() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入你想要订购的车：");
        try {
            return reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }


    public static void main(String[] args) {
//        new OrderCars().orderCars();
        new OrderCars().orderCarByFactory();
    }
}
